package com.cm.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cm.course.entity.WrongQuestion;
import com.cm.course.mapper.WrongQuestionMapper;
import com.cm.course.service.WrongService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author xdf
 * @date 2023-06-22
 */
@Service
public class WrongServiceImpl implements WrongService {

    @Resource
    WrongQuestionMapper wrongQuestionMapper;


    @Override
    public Map<String, Integer> getWrongQuestionNumberByDifficulty(Long studentId) {
        QueryWrapper<WrongQuestion> wrongQuestionQueryWrapper = new QueryWrapper<>();
        wrongQuestionQueryWrapper
                .eq("stu_id", studentId);
        Map<String, Integer> result = new HashMap<>();
        // key为level，value为该难度level下的所有错题记录
        Map<String, List<WrongQuestion>> levelMap =
                wrongQuestionMapper.selectList(wrongQuestionQueryWrapper).stream()
                        .collect(Collectors.groupingBy(WrongQuestion::getLevel));
        Set<String> levelKeys = levelMap.keySet();
        int total=0;
        for (String level : levelKeys) {
            int size=levelMap.get(level).size();
            total=total+size;
            result.put(level, size);
        }
        result.put(String.valueOf(-1),total);
        return result;
    }

    @Override
    public List<String> getPoints(Long studentId) {
        QueryWrapper<WrongQuestion> wrongQuestionQueryWrapper = new QueryWrapper<>();
        wrongQuestionQueryWrapper
                .eq("stu_id", studentId);
        // 只获取WrongQuestion的points这一列，并且去重
        List<String> points=wrongQuestionMapper.selectList(wrongQuestionQueryWrapper).stream().map(WrongQuestion::getPoints).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
        return points;
    }

    @Override
    public Page<WrongQuestion> getWrongQuestion(Long studentId, Long current, Long size) {
        QueryWrapper<WrongQuestion> wrongQuestionQueryWrapper = new QueryWrapper<>();
        wrongQuestionQueryWrapper
                .eq("stu_id", studentId);
        Page<WrongQuestion> page=new Page<>(current,size);
        wrongQuestionMapper.selectPage(page,wrongQuestionQueryWrapper);
        return page;
    }
}
